package max.ubitest.pipe.commands
{
	import max.ubitest.pipe.data.CellData;
	import max.ubitest.pipe.data.ComponentData;
	import max.ubitest.pipe.locators.ComponentsLocator;
	
	import org.robotlegs.mvcs.Command;
	
	public class ClearCommand extends Command
	{
		[Inject]
		public var locator:ComponentsLocator;
		
		public function ClearCommand()
		{
			super();
		}
		
		override public function execute():void{
			locator.reset();
			while(locator.mappedComponents.length > 0){
				var cell:CellData = locator.mappedComponents.removeItemAt(0) as CellData;
				addBack(cell.componentData);
				cell.componentData = null;
			}
		}
		
		private function addBack(cd:ComponentData):void{
			for(var i:int = 0;i<locator.components.length;i++){
				if(locator.components.getItemAt(i).type == cd.type){
					locator.components.getItemAt(i).amount ++;
				}
			}
		}
	}
}